package test;

public class Stack {
//-----栈
    //算法实现中缀表达式计算不包含括号----（）
    String[] array;
    int max;
    int topPointer=-1;
    //初始化容量
    public Stack(int max){
        this.max=max;
        this.array=new String[max];
    }
    //入栈
    public void push(String data){
        if(topPointer==max-1){
            throw new RuntimeException("栈已满");
        }
        topPointer++;
        array[topPointer]=data;
    }
    //出栈
    public void pop(){
        if(topPointer==-1){
            throw new RuntimeException("栈已空");
        }
        array[topPointer]=null;
        topPointer--;
    }

    //输入字符串得到运算结果
    public int getResult(String stringData){
        //符号栈

        Stack symbolStack = new Stack(this.max);
        while(true){
            if(stringData.length()==-1){break;}
            String data=getdata(stringData);
            if(isNumeric(data)){
                //如果为数字入当前栈（数字栈）
                this.push(data);
                stringData=truncationString(stringData,data);
                data=getdata(stringData);
            }else{
                if(symbolStack.topPointer==-1){
                    //符号栈为空直接入栈（符号栈）
                    symbolStack.push(data);
                    stringData=truncationString(stringData,data);
                    data=getdata(stringData);

                }
                else{
                    //符号栈栈顶 符号优先级 //入栈符号优先级
                    int value=getPriority(symbolStack.array[symbolStack.topPointer]);//------栈顶符号
                    int value2=getPriority(data);//-------------当前需要入栈的符号
                    //入栈符号优先级小于或等于栈顶符号优先级
                    if(value>=value2){
                        int zhi = getValue(array[topPointer-1],array[topPointer],symbolStack.array[symbolStack.topPointer]);
                        this.pop();
                        this.pop();
                        this.push(zhi+"");
                        symbolStack.pop();
                        symbolStack.push(data);
                        stringData=truncationString(stringData,data);
                        data=getdata(stringData);


                    }else{
                        symbolStack.push(data);
                        stringData=truncationString(stringData,data);
                        data=getdata(stringData);

                    }

                }


            }

        }
        int returnValue=0;
        while(true){
            if(symbolStack.topPointer==-1){return Integer.parseInt(this.array[topPointer]);}
            returnValue=getValue(this.array[topPointer-1],this.array[topPointer],symbolStack.array[topPointer]);
            this.pop();
            this.pop();
            this.push(returnValue+"");
            symbolStack.pop();


        }




    }
    //输入字符串返回字符串的第一个数值或第一个运算符
    private String getdata(String data){
        String zhi="";
        //遍历字符串每一个字符
        for(int i=0;i<data.length();i++){
            Character ca=data.charAt(i);
            //下标字符是数字
            if(Character.isDigit(ca)){
                zhi=ca+zhi;
            }
            //下标是运算符
            if(!Character.isDigit(ca)){
                //zhi为空串
                if(zhi==""){
                    zhi=ca+"";
                    return zhi;
                }
                //zhi不为空串
                return zhi;

            }
        }
        return zhi;
    }
    //判断字符串是否全部为数字
    public  boolean isNumeric(String str){
        for (int i = str.length();--i>=0;){
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
        return true;
    }
    //截断字符串 获取新字符串
    public String truncationString(String primaryData,String data){
        //截取字符串 从指定下标截取到最后
        String i=primaryData.substring(data.length());
        return i;
    }
    //获取符号优先级
    public int getPriority(String symbol){
        if(symbol=="*"||symbol=="/"){
            return 1;
        }
        return 0;
    }
    //两个string类型的数值和一个string类型的符号做运算
    public int getValue(String i,String j,String k){
        int i1=Integer.parseInt(i);
        int j1=Integer.parseInt(j);
        switch (k){
            case "+":
                return i1+j1;
            case "-":
                return i1-j1;
            case "*":
                return i1*j1;
            case "/":
                return i1/j1;

            default:
                throw new RuntimeException("非+-*/");

        }
    }

}
